package com.doujiang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doujiang.domain.Movie;
import com.doujiang.domain.dto.PageResult;
import com.doujiang.domain.dto.jhjMovieDTO;

import com.doujiang.mapper.jhjMovieMapper;
import com.doujiang.service.jhjIMovieService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电影Service业务层处理
 * 
 * @author 王伟舰
 * @date 2025-07-19
 */
@Service
public class jhjMovieServiceImpl extends ServiceImpl<jhjMovieMapper, Movie> implements jhjIMovieService
{
    @Resource
    private jhjMovieMapper jhjMovieMapper;

    /**
     * 贝叶斯算法中的最小投票阈值，可以通过配置注入，这里写死方便示范
     */

    @Override
    public double getGlobalAvgScore() {
        Double avg = jhjMovieMapper.selectGlobalAvgScore();
        return avg != null ? avg : 5.0;  // 如果没有数据，默认5.0分
    }

    @Override
    public List<jhjMovieDTO> getTopSuspenseMovies(int minVotes) {
        double globalAvg = getGlobalAvgScore();
        return jhjMovieMapper.selectTopSuspenseMovies(minVotes, globalAvg);
    }

    @Override
    public List<jhjMovieDTO> getHighScoreMovies(int minVotes) {
        double globalAvg = getGlobalAvgScore();
        return jhjMovieMapper.selectHighScoreMovies(minVotes, globalAvg);
    }

    @Override
    public List<jhjMovieDTO> getLatestMovies(int minVotes) {
        double globalAvg = getGlobalAvgScore();
        return jhjMovieMapper.selectLatestMovies(minVotes, globalAvg);
    }

    @Override
    public List<jhjMovieDTO> getActionThrillerMovies(int minVotes) {
        double globalAvg = getGlobalAvgScore();
        return jhjMovieMapper.selectActionThrillerMovies(minVotes, globalAvg);
    }

    @Override
    public List<jhjMovieDTO> getFantasySciFiMovies(int minVotes) {
        double globalAvg = getGlobalAvgScore();
        return jhjMovieMapper.selectFantasySciFiMovies(minVotes, globalAvg);
    }

    @Override
    public List<jhjMovieDTO> getNolanMovies(int minVotes) {
        double globalAvg = getGlobalAvgScore();
        return jhjMovieMapper.selectNolanMovies(minVotes, globalAvg);
    }

    @Override
    public List<jhjMovieDTO> getChineseMovies(int minVotes) {
        double globalAvg = getGlobalAvgScore();
        return jhjMovieMapper.selectChineseMovies(minVotes, globalAvg);
    }
    @Override
    public List<jhjMovieDTO> selectMoreMovies(String name, int minVotes, double globalAvg, int page, int size) {
        int offset = (page - 1) * size;
        return jhjMovieMapper.selectMoreByType(name, minVotes, globalAvg, offset, size);
    }
    @Override
    public PageResult<jhjMovieDTO> getMoviesByType(String name, int minVotes, double globalAvg, int page, int size) {
        System.out.println("Service层处理 - name: " + name + ", minVotes: " + minVotes + ", globalAvg: " + globalAvg + ", page: " + page + ", size: " + size);
        int offset = (page - 1) * size;
        List<jhjMovieDTO> records = jhjMovieMapper.selectMoreByType(name, minVotes, globalAvg, offset, size);
        int total = jhjMovieMapper.countByType(name, minVotes);
        System.out.println("查询结果 - 记录数: " + records.size() + ", 总数: " + total);
        PageResult<jhjMovieDTO> result = new PageResult<>();
        result.setRecords(records);
        result.setTotal(total);
        return result;
    }
}
